import { MenuData } from "@/models/menu";
import { allRoutes } from "@/router";
import { ChildAppRoute, RouteMeta } from "@/router/routeMeta";
import { RouteConfig } from "vue-router";

// 子应用菜单数据转换路由数据
function menuToRoute_c(menuData: MenuData, parentKeys: string[] = [],basePath?:string) {
  let result: ChildAppRoute;
  let nparentKeys = [...parentKeys];
  // let urls=menuData.uri.split("/") 

  if (menuData.parentId != 0) nparentKeys.push(`${menuData.parentId}`);
  result = {
    title: menuData.menuname,
    path:menuData.uri,
    defaultPath:basePath?`${basePath}${menuData.uri}`: menuData.uri,
    menuKey: `${menuData.id}`,
    parentKeys: nparentKeys,
    children: menuData.menus?.map(childItem => {
      return menuToRoute_c(childItem, parentKeys,basePath);
    }),
    isChildAppRoute:true,
    activeMenu: false,
    hidden: false
  };
  return result;
}

export function menusToRoutes_c(
  menusData: Array<MenuData>,
  parentKeys: string[] = [],
  basePath?:string
) {
  //   子应用菜单数组数据转换路由数组数据
  let result: Array<ChildAppRoute>;
  result = menusData.map(item => {
    return menuToRoute_c(item, parentKeys,basePath);
  });
  return result;
}

export function dataToRoutes_m(
  menusData: Array<MenuData>,
  routes: RouteConfig[]
) {
  //   主应用权限菜单数据转换路由数组数据

  let result: RouteConfig[] = [];
  routes.forEach(routeitem => {
    let newroute = { ...routeitem };
    let metaData = routeitem.meta as RouteMeta;
    let menuIndex = menusData.findIndex(
      menuitem => `${menuitem.id}` === metaData.menuKey
    );
    if (menuIndex !== -1) {
      let menuitem = menusData[menuIndex];
      if (routeitem.children && menuitem.menus)
        newroute.children = dataToRoutes_m(menuitem.menus, routeitem.children);

      result.push(newroute);
    }
  });
  return result;
}
